// OGoMojo
// Whitemice.MOGI.HashCacheEntry
// License: LGPL
// Copyright: Whitemice Consulting, 2006
// Author(s): Adam Tauno Williams (awilliam@whitemice.org)
using System;
using System.Collections;

namespace Whitemice.MOGI 
{	
	internal class HashCacheEntry
	{
		protected WeakReference	weakReference;
		protected object			strongReference;
		protected DateTime			lastPut;
		protected CachePolicy		policy;
		protected DateTime			lastGet;
		protected DateTime			origin;
		protected string			modified;
		protected string			name;

		
		public HashCacheEntry(object document, CachePolicy policy)
		{
			this.policy = policy;
			this.origin = DateTime.Now;
			this.modified = null;
			Store(document);
		}
		
		public bool IsAlive()
		{
			return weakReference.IsAlive;
		}
		
		public object Payload
		{
			get
			{
				if (IsAlive())
					return weakReference.Target;
				else return null;
			}
		}
		
		internal void Store(object document)
		{
			if(document is Whitemice.MOGI.IDocument)
				name = ((IDocument)document).Name;
				else name = null;
			weakReference = new WeakReference(document);
			if (policy == CachePolicy.Strong)
				strongReference = document;
		}
				
		public DocumentType Type
		{
			get
			{
				if (IsAlive())
					if (weakReference.Target is IDocument)
						return MOGI.DetermineType(((IDocument)weakReference.Target).Name);
				return DocumentType.Undefined;
			}
		}
		
		public string Name
		{
			get	{ return name; }
		}
		
		public bool IsModified 
		{
			get
			{
				if (modified == null)
					return false;
				return true;
			}
		}
		
		public void Saved()
		{
			modified = null;
		}
		
		public DateTime Modified
		{
			get { return Convert.ToDateTime(modified); }
			set { modified = value.ToString(); }
		}
	}
}